package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.User;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.pet.domain.OrderPetAcquisition;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMessage;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.OrderPetAcquisitionQuery;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition, OrderPetAcquisitionQuery> implements IOrderPetAcquisitionService {
    @Resource
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Resource
    private SearchMasterMsgMapper msgMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private PetMapper petMapper;
    @Override
    public JsonResult jieDan(SearchMasterMsg msg) {
        //1.修改msgshopid的状态
        msg.setShopId(LoginInfoUtil.getShopId());
        Long shopId = LoginInfoUtil.getShopId();
        //查公司

        msgMapper.updateByPrimaryKeySelective(msg);
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        //2.先查询
        SearchMasterMsg searchMasterMsg = msgMapper.selectByPrimaryKey(msg.getId());
        //2.拷贝数据到订单表
        OrderPetAcquisition orderPetAcquisition = BeanUtil.copyProperties(searchMasterMsg, OrderPetAcquisition.class);
        orderPetAcquisition.setDigest(msg.getNote());
        orderPetAcquisition.setEmpId(msg.getEmpId());
        orderPetAcquisition.setMsgId(msg.getId());
        orderPetAcquisition.init();
        //这里的地址改为收购公司的地址
        orderPetAcquisition.setAddress(shop.getAddress());
        orderPetAcquisitionMapper.insertSelective(orderPetAcquisition);
        return JsonResult.createSuccess();
    }
    @Override
    public PageList<OrderPetAcquisition> findPage(OrderPetAcquisitionQuery orderPetAcquisitionQuery) {
        PageHelper.startPage(orderPetAcquisitionQuery.getPage(),orderPetAcquisitionQuery.getSize());
        List<OrderPetAcquisition> orderPetAcquisitions = orderPetAcquisitionMapper.findPage(orderPetAcquisitionQuery);
        PageInfo<OrderPetAcquisition> pageInfo = new PageInfo<>(orderPetAcquisitions);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public JsonResult adoptshenhe(OrderPetAcquisition orderPetAcquisition) {
        //查找msg订单表数据
        Long msgId = orderPetAcquisition.getMsgId();
        SearchMasterMsg searchMasterMsg = msgMapper.selectByPrimaryKey(msgId);
        //开始替换
        Pet pet = BeanUtil.copyProperties(searchMasterMsg, Pet.class);
        //pet的默认状态下架
        pet.setState(0);
        pet.setTypeId(orderPetAcquisition.getPetType());
        //设置costprice
        pet.setCostprice(orderPetAcquisition.getPrice());
        pet.setId(null);
        pet.setAdoptId(orderPetAcquisition.getId());
        //把user的count加钱
        //查user
        Long userId = orderPetAcquisition.getUserId();
        User user = userMapper.selectByPrimaryKey(userId);
        Double count= user.getCount() + orderPetAcquisition.getPrice();
        user.setCount(count);
        //更新user表
        userMapper.updateByPrimaryKeySelective(user);
        //新增pet表
        petMapper.insertSelective(pet);
        Long petId = pet.getId();
        //开始修改oredr
        orderPetAcquisition.setPetId(petId);
        orderPetAcquisitionMapper.updateByPrimaryKeySelective(orderPetAcquisition);
        return JsonResult.createSuccess("收购成功");
    }

    @Override
    public JsonResult adoptCancel(OrderPetAcquisition orderPetAcquisition) {
        Long msgId = orderPetAcquisition.getMsgId();
        msgMapper.deleteByPrimaryKey(msgId);
        Long id = orderPetAcquisition.getId();
        orderPetAcquisitionMapper.deleteByPrimaryKey(id);
        return JsonResult.createSuccess("驳回订单成功");
    }
}
